package com.top10.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.top10.bean.JwtUtil;
import com.top10.bean.PageBean;
import com.top10.bean.ResponseBean;
import com.top10.common.Const;
import com.top10.common.OrderConst;
import com.top10.entity.*;
import com.top10.entity.vo.UserVo;
import com.top10.service.ICouponService;
import com.top10.service.ICouponUserService;
import com.top10.service.IUserService;
import com.top10.service.IUserWithdrawalService;
import com.top10.utils.GetUuid;
import com.top10.utils.OSSFilePath;
import com.top10.utils.OSSUploadUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 用户Controller
 *
 * @author ellen
 * @version 1.0
 * @date 2018/9/27 17:21
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    IUserService userService;

    @Autowired
    IUserWithdrawalService userWithdrawalService;

    JwtUtil jwtUtil = new JwtUtil();
    @Autowired
    private ICouponUserService iCouponUserService;
    @Autowired
    private ICouponService iCouponService;

    /**
     * 个人中心
     *
     * @param request
     * @return
     */
    @RequestMapping("/manager/userCenter")
    public ResponseBean userCenter(HttpServletRequest request,PageBean pageBean) {
        String uuid = JwtUtil.getUuidByToken(request);
        UserVo userVo = userService.selectUserVo(uuid);
       // Integer couponnumber=iCouponUserService.selectCount(new EntityWrapper<CouponUser>().eq("isUsed", Const.IsNotDelete).eq("userId",uuid));

        Page page= PageHelper.startPage(pageBean.getPageNum(),pageBean.getPageSize());
        List<Map<String,Object>> coupon=iCouponService.couponNotUser(uuid,2);

        pageBean=new PageBean(pageBean.getPageNum(),pageBean.getPageSize(), (int) page.getTotal());
        Integer  couponnumber=pageBean.getCount();
        return ResponseBean.createSuccess().addData("userVo", userVo).addData("couponnumber",couponnumber);
    }

    /**
     * 编辑个人资料
     */
    @RequestMapping("/manager/updateuser")
    public ResponseBean updateUser(HttpServletRequest request, User user, @RequestParam(value = "image", required = false) MultipartFile image) {
        String uuid = JwtUtil.getUuidByToken(request);/*user.setUuid(uuid);*/
      /*  String uuid = "f0e225b41d4a4715b942f9e1897a1c8b";*/
        String fileName = new GetUuid().newId()+".png";
        String imageSavePath = OSSFilePath.HEADE + fileName;
        OSSUploadUtils ossUploadUtils = new OSSUploadUtils();
        ossUploadUtils.uploadFile(imageSavePath, image);
        ossUploadUtils.closeOssClient();
        String imagepath = OSSFilePath.OSSDOMAIN + "/userhead/" + fileName;
        user.setHeadSculpture(imagepath);
        user.setUuid(uuid);
        Integer rest = userService.updateUser(user);
        if (rest != 0) {
            return ResponseBean.createSuccess("编辑成功").addData("imagepath",imagepath);
        } else {
            return ResponseBean.createError("编辑失败");
        }
    }



    /**
     * 个人资料
     *
     * @param request
     * @return
     */
    @RequestMapping("/manager/userInfo")
    public ResponseBean userInfo(HttpServletRequest request) {
        String uuid = JwtUtil.getUuidByToken(request);
        User user = userService.selectOne(new EntityWrapper<User>().eq("uuid", uuid));
        return ResponseBean.createSuccess().addData("user", user);
    }


    /**
     * 查询用户店铺收藏
     *
     * @param request
     * @return
     */
    @RequestMapping("/manager/selectShopCollect")
    public ResponseBean selectShopCollect(HttpServletRequest request, PageBean pageBean) {
        String userUuid = JwtUtil.getUuidByToken(request);
        Page page = PageHelper.startPage(pageBean.getPageNum(), pageBean.getPageSize());
        List<Shop> shops = userService.selectShopCollect(userUuid);
        pageBean = new PageBean(page.getPageNum(), page.getPageSize(), (int) page.getTotal());

        return ResponseBean.createSuccess().addData("shops", shops).addData("pageBean", pageBean);
    }

    /**
     * 查询用户商品收藏
     *
     * @param request
     * @return
     */
    @RequestMapping("/manager/selectGoodsCollect")
    public ResponseBean selectGoodsCollect(HttpServletRequest request, PageBean pageBean) {
        String userUuid = JwtUtil.getUuidByToken(request);
        Page page = PageHelper.startPage(pageBean.getPageNum(), pageBean.getPageSize());
        List<Goods> goods = userService.selectGoodsCollect(userUuid);
        pageBean = new PageBean(page.getPageNum(), page.getPageSize(), (int) page.getTotal());

        return ResponseBean.createSuccess().addData("goods", goods).addData("pageBean", pageBean);
    }


  /*  *//**
     * 用户浏览记录
     *
     * @param request
     * @return
     *//*
    @RequestMapping("/manager/userBrowse")
    public ResponseBean userBrowse(HttpServletRequest request) {
//        PageHelper.startPage(1, 8);
        String userUuid = JwtUtil.getUuidByToken(request);
        List<Goods> goods = userService.userBrowse(userUuid);

        return ResponseBean.createSuccess().addData("goods", goods);
    }
*/
    /**
     * 删除银行卡
     */
    @RequestMapping("/manager/bank")
    public ResponseBean deletedBank(HttpServletRequest request) {
        String userUuid = JwtUtil.getUuidByToken(request);
        /* String userUuid="090e97c21902498c9864129382321a7d";*/
        Integer rest = userService.deleteBank(userUuid);
        if (rest != 0) {
            return ResponseBean.createSuccess().setMessage("删除成功");
        } else {
            return ResponseBean.createError("没有银行卡删除");
        }
    }

    /**
     * 添加银行卡信息
     */
    @RequestMapping("/manager/bankone")
    public ResponseBean addBank(User user, HttpServletRequest request) {
        String userUuid = JwtUtil.getUuidByToken(request);
        /* String userUuid="090e97c21902498c9864129382321a7d";*/
        User user1 = userService.selectOne(new EntityWrapper<User>().eq("uuid", userUuid));
        if (user1.getBankName()==null || user1.getBankName().equals("")) {
            user.setUuid(userUuid);
            userService.updateBnak(user);
            return ResponseBean.createSuccess("添加银行卡成功");
        } else {
            return ResponseBean.createSuccess("你已经有银行卡,不需要添加了").addData("user",user);
        }

    }
    /**
     * 银行卡列表
     */
    @RequestMapping("/manager/banklist")
    public ResponseBean BankList(HttpServletRequest request){
        String userUuid = JwtUtil.getUuidByToken(request);
        User user1 = userService.selectOne(new EntityWrapper<User>().eq("uuid", userUuid));
        if(user1.getBankAccountName()==null || user1.getBankAccountName().equals("")){
            return ResponseBean.createSuccess("您还没有添加银行卡");
        }
        User user=new User();
        user.setBankAccountName(user1.getBankAccountName());
        user.setBankAccountNo(user1.getBankAccountNo());
        user.setBankName(user1.getBankName());
        user.setBankCity(user1.getBankCity());
        return ResponseBean.createSuccess().addData("user",user);
    }

    /**
     * 获取冻结钱和可提现钱
     */
    @RequestMapping("/manager/getmoney")
    public ResponseBean getMoney(HttpServletRequest request) {
        String userUuid = JwtUtil.getUuidByToken(request);
        /* String userUuid="b17200315c91433688e579f5acdcabe0";*/
        User user = userService.selectOne(new EntityWrapper<User>().eq("uuid", userUuid));
        List<UserWithdrawal> userWithdrawals = userWithdrawalService.selectList(new EntityWrapper<UserWithdrawal>()
                .eq("uid", userUuid)
                .eq("status", OrderConst.withdrawalstatus0));
        double total = 0.00;//冻结的钱
        double total1 = 0.00;
        for (UserWithdrawal uw : userWithdrawals) {
            double money = uw.getMoney().doubleValue();
            total += money;
        }
        if(user!=null) {
            total1 = user.getMoney().doubleValue();//钱包金额
        }

        double total2 = total1 - total;//可以用的钱
        if(total2<0){
            total2=0;
        }

        BigDecimal bd = new BigDecimal(total2);
        double total3 = bd.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();

        BigDecimal bd1 = new BigDecimal(total);
        double total4 = bd1.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();

        return ResponseBean.createSuccess().addData("total", total3).addData("total1", total4);
    }
}
